package com.二叉树2;

import java.util.LinkedList;
import java.util.Stack;

/**
 * Maximum Depth of Binary Tree
 *
 * Solution
 * Given a binary tree, find its maximum depth.
 *
 * The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
 *
 * Note: A leaf is a node with no children.
 *
 * Example:
 *
 * Given binary tree [3,9,20,null,null,15,7],
 *
 *     3
 *    / \
 *   9  20
 *     /  \
 *    15   7
 * return its depth = 3.
 * https://leetcode.com/explore/learn/card/data-structure-tree/17/solve-problems-recursively/535/
 */
public class 最大深度 {
    static class Solution {
        public int maxDepth(TreeNode root) {
            int maxDep = 0;
            if(root == null){
                return  maxDep;
            }
            LinkedList<TreeNode> quene = new LinkedList<>();
            quene.add(root);
            while (!quene.isEmpty()) {
                int size = quene.size();
                for (int i = 0; i < size; i++) {
                    TreeNode tmp = quene.pop();
                    if (tmp.left != null) {
                        quene.add(tmp.left);
                    }
                    if (tmp.right != null) {
                        quene.add(tmp.right);
                    }
                }
                maxDep ++;
            }
            return  maxDep;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int num = solution.maxDepth(new TreeNode(new int[]{1,2,3,4,5,6,7}));
    }
}
